package com.dgut.demo.service.impl;

import com.dgut.demo.bean.DispatchMessage;
import com.dgut.demo.bean.EmployeesMessage;
import com.dgut.demo.bean.GoodsMessage;
import com.dgut.demo.bean.User;
import com.dgut.demo.controller.GoodsController;
import com.dgut.demo.controller.LogsController;
import com.dgut.demo.mapper.DispatchMapper;
import com.dgut.demo.mapper.EmployeesMapper;
import com.dgut.demo.mapper.GoodsMapper;
import com.dgut.demo.service.DispatchService;
import com.mysql.cj.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Auther 虎
 * @Data 2021/2/24 15:03
 * @Version 1.0
 */
@Service
public class DispatchServiceImpl implements DispatchService{
    @Autowired
    private DispatchMapper dispatchMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private EmployeesMapper employeesMapper;
    @Autowired
    private User user;
    @Autowired
    private LogsController logsController;

    @Override
    public List<DispatchMessage> getAllDispatchMessage() {
        return dispatchMapper.getAllDispatchMessage();
    }

    @Override
    public List<DispatchMessage> getDispatchMessageByDid(String did) {
        return dispatchMapper.getDispatchMessageByDid(did);
    }

    @Override
    public List<DispatchMessage> getDispatchMessageByVid(String dvid) {
        return dispatchMapper.getDispatchMessageByVid(dvid);
    }

    @Override
    public List<DispatchMessage> getDispatchMessageByPhone(String dvphone) {
        return dispatchMapper.getDispatchMessageByPhone(dvphone);
    }

    @Override
    public List<DispatchMessage> getDispatchMessageByEname(String dename) {
        return dispatchMapper.getDispatchMessageByEname(dename);
    }

    @Override
    public List<DispatchMessage> getDispatchMessageByGname(String dgname) {
        return dispatchMapper.getDispatchMessageByGname(dgname);
    }

    @Override
    public int addOneDispatchMessage(DispatchMessage dispatchMessage) {
        GoodsMessage goodsMessage = new GoodsMessage();
        EmployeesMessage employeesMessage = new EmployeesMessage();
        goodsMessage = goodsMapper.getGoodsMessageById(dispatchMessage.getDgid()).get(0);
        employeesMessage = employeesMapper.getEmployeesMessageById(dispatchMessage.getDeid()).get(0);
        if(goodsMessage.getGcounts()>=dispatchMessage.getDcounts()){

            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyMMddHHmmss");
            int random = (int)(Math.random()*10);
            String did='6'+ dateFormat1.format(date) + random;
            dispatchMessage.setDid(did);
            SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dtime = dateFormat2.format(date);
            dispatchMessage.setDtime(dtime);
            double dmoney = goodsMessage.getGprice() * dispatchMessage.getDcounts();
            dispatchMessage.setDmoney(dmoney);
            int flag = dispatchMapper.addOneDispatchMessage(dispatchMessage);
            if(flag == 1){
                goodsMessage.setGcounts(goodsMessage.getGcounts()-dispatchMessage.getDcounts());
                goodsMapper.updateOneGoodsMessage(goodsMessage);

                //log
                String actions = "新增发货单【编号:"+did+",会员编号:"+dispatchMessage.getDvid()+",商品:"+goodsMessage.getGname()+",数量:"+dispatchMessage.getDcounts()+",总额:"+dmoney+",负责人:"+employeesMessage.getEname()+"】";
                String userId = user.getUserId();
                logsController.addOneLogsMessage(actions,userId);
                return 1;
            }else{
                return 0;
            }
        }else{
            return 0;
        }
    }
}
